import { success, fail, ProxyMode } from "../utils/base";

// 拦截api操作，转发后端接口
export default defineEventHandler(async (event) => {
  // 获取路由参数
  const apiname = getRequestURL(event);
  if (apiname.pathname === "/fx/sendProxy") {
    // console.log("--请求地址：", apiname.pathname);
    // 获取请求参数 post
    const body = await readBody(event);
    // console.log("-- serve 请求代理:", body);
    const options: any = {
      responseType: "json",
    };
    options.headers = {
      "content-type": "application/json",
      accept: "application/json",
    };
    options.method = "post";
    options.body = JSON.stringify(body.body);
    const resBody = await $fetch(body.url, options)
      .then((res) => res)
      .catch((err) => {
        return fail({ msg: "服务端代理转发错误", err });
      });
    return success(resBody);
  }
});
